import {request} from '/utils/http'
import {requestUrl} from '/config';

Page({
  data: {
    id:0,
    order_id: 0,
    form:{
      apply_note:'',
      apply_images:[],
      person:'',
      mobile:''
    },
    imgList:[],
    success: false,
    action: requestUrl + '/upload/file',
    company_name:'',
    pay_method:''
  },
  onLoad(query) {
    let userinfo = my.getStorageSync({
      key: 'userInfo',
    });

    let company_name = my.getStorageSync({
      key: 'company_name',
    }).data;
    console.log(company_name)
    let form = this.data.form;
    form.person = userinfo.data.name;
    form.mobile = query.mobile;

    this.setData({
      order_id: query.order_id,
      form,
      company_name,
      pay_method:query.pay_method
    })
  },
  toDetail() {
    my.navigateTo({
      url: '/pages/complaint/detail/detail?id=' + this.data.id
    });
  },
  toHome() {
    my.switchTab({url: '/pages/index/index'})
  },
  bindKeyInputName(e) {
    let form = this.data.form;
    form.apply_note = e.detail.value;
    this.setData({
      form
    })
  },
  onInputPerson(e){
    let form = this.data.form;
    form.person = e.detail.value;
    this.setData({
      form
    })
  },
  onInputMobile(e){
    let form = this.data.form;
    form.mobile = e.detail.value;
    this.setData({
      form
    })
  },
  save(){
    if (!this.data.form.apply_note) {
      my.showToast({content: '请输入原因'})
      return
    }

    if (!this.data.form.person) {
      my.showToast({content: '请输入联系人'})
      return
    }

    if (!this.data.form.mobile) {
      my.showToast({content: '请输入联系电话'})
      return
    }
    let form = this.data.form;
    form['order_id'] = this.data.order_id
    request('/order/request', 'post', form).then(res => {
      this.setData({
        success: true,
        id:res.data.id
      })
    })
  },
  onDelete(e){
    console.log(e)
  },
  onUploadChange(list){
    console.log(list)
    let apply_images = [];
    list.forEach(item => {
      if (item.url) {
        apply_images.push(item.url);

      }
    });
    let form = this.data.form;
    form.apply_images = apply_images
    this.setData({
      form
    })
  },
  onAfterUpload(res){
    return new Promise((resolve) => {
      if (res.code == 200) {
        if (res.data.url) {
          resolve(res.data.url)
        }
      } else {
        resolve('')
      }
    })
  }
});
